<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <!--<script src="./js/jquery-2.1.1.js"></script>-->
    <script src="./js/underscore.js"></script>
    <script src="./js/backbone.js"></script>

    <!--模型-->
    <script>
        Person = Backbone.Model.extend({
            validate: function (attr) {
                console.log('validate age');
                if (attr.age < 0) {
                    console.log('age <0');
                    alert('age < 0');
                    return "age must > 0";
                }
            },

            initialize: function () {
                console.log("init");

                this.on("change:name", function (model) {
                    var name = model.get('name');
                    console.log('name change to:' + name);
                });

                this.bind('error', function (model, error) {
                    console.log('error: ' + error);
                });
            },

            defaults: {
                name: 'no-name',
                age: 0,
                child: 'none'
            },

            print: function () {
                var str = 'name=' + this.get('name');
                str += ',age=' + this.get('age');
                str += ',child=' + this.get('child');
                console.log(str);
            }
        });

        var jim = new Person({name: 'jim', age: 23, child: 'bobo'});
        jim.print();

        var nobody = new Person
        nobody.print();
        nobody.set({name: 'tom'});
        nobody.print();

        var xx = new Person({name: 'abc', age: -1});
        xx.print();
        xx.save();
    </script>


</head>
<body>
hello backbone


</body>
</html>